package com.manli.api.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.manli.api.base.exception.MyException;
import com.manli.api.dto.*;
import com.manli.api.pojo.*;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ObjectEmptyUtil {
    public static void branchIsEmpty(Branch branch){
        if(StringUtils.isEmpty(branch.getBranchName())){
            throw new MyException(-1,"机构名称为空");
        }
        if(StringUtils.isEmpty(branch.getBranchPhone())){
            throw new MyException(-1,"机构号码为空");
        }
        if(StringUtils.isEmpty(branch.getLatitude())){
            throw new MyException(-1,"纬度为空");
        }
        if(StringUtils.isEmpty(branch.getLongitude())){
            throw new MyException(-1,"经度为空");
        }
        if(StringUtils.isEmpty(branch.getBranchAddress())){
            throw new MyException(-1,"总店地址为空");
        }
        if(StringUtils.isEmpty(branch.getProvincesCode())){
            throw new MyException(-1,"省级编码为空");
        }
        if(StringUtils.isEmpty(branch.getCitiesCode())){
            throw new MyException(-1,"市级编码为空");
        }
        if(StringUtils.isEmpty(branch.getAreasCode())){
            throw new MyException(-1,"区级编码为空");
        }
        if(StringUtils.isEmpty(branch.getStreetsCode())){
            throw new MyException(-1,"街道镇级编码为空");
        }
        if(StringUtils.isEmpty(branch.getVillagesCode())){
            throw new MyException(-1,"村级编码为空");
        }
        if(branch.getStatus() == null){
            throw new MyException(-1,"状态为空");
        }

    }

    public static void partnerIsEmpty(Partner partner){
        if(partner.getBranchId() == null){
            throw new MyException(-1,"总店机构为空");
        }
        if(StringUtils.isEmpty(partner.getPartnerName())){
            throw new MyException(-1,"机构名称为空");
        }
        if(StringUtils.isEmpty(partner.getPartnerPhone())){
            throw new MyException(-1,"机构号码为空");
        }
        if(StringUtils.isEmpty(partner.getLatitude())){
            throw new MyException(-1,"纬度为空");
        }
        if(StringUtils.isEmpty(partner.getLongitude())){
            throw new MyException(-1,"经度为空");
        }
        if(StringUtils.isEmpty(partner.getPartnerAddress())){
            throw new MyException(-1,"分店地址为空");
        }
        if(StringUtils.isEmpty(partner.getProvincesCode())){
            throw new MyException(-1,"省级编码为空");
        }
        if(StringUtils.isEmpty(partner.getCitiesCode())){
            throw new MyException(-1,"市级编码为空");
        }
        if(StringUtils.isEmpty(partner.getAreasCode())){
            throw new MyException(-1,"区级编码为空");
        }
        if(StringUtils.isEmpty(partner.getStreetsCode())){
            throw new MyException(-1,"街道镇级编码为空");
        }
        if(StringUtils.isEmpty(partner.getVillagesCode())){
            throw new MyException(-1,"村级编码为空");
        }
        if(partner.getStatus() == null){
            throw new MyException(-1,"状态为空");
        }

    }

    public static void userIsEmpty(User user){
        if(user.getBranchId() == null){
            throw new MyException(-1,"总店机构为空");
        }
        if(StringUtils.isEmpty(user.getPassword())){
            throw new MyException(-1,"密码为空");
        }
        if(StringUtils.isEmpty(user.getUsername())){
            throw new MyException(-1,"用户名为空");
        }
        if(user.getStatus() == null){
            throw new MyException(-1,"状态为空");
        }
        if(StringUtils.isEmpty(user.getRealName())){
            throw new MyException(-1,"真实姓名为空");
        }

    }

    public static void userIsEmptyPass(User user){
        if(StringUtils.isEmpty(user.getPassword())){
            throw new MyException(-1,"密码为空");
        }
        if(StringUtils.isEmpty(user.getUsername())){
            throw new MyException(-1,"用户名为空");
        }
        if(StringUtils.isEmpty(user.getUserPhone())){
            throw new MyException(-1,"手机号为空");
        }
        if(StringUtils.isEmpty(user.getPhoneCode())){
            throw new MyException(-1,"验证码为空");
        }


    }

    public static void passwordUpdateLogin(QueryParamsUserPass pass){
        if(StringUtils.isEmpty(pass.getLastPassword())){
            throw new MyException(-1,"旧密码为空");
        }
        if(StringUtils.isEmpty(pass.getNewPassword())){
            throw new MyException(-1,"新密码为空");
        }
        if(StringUtils.isEmpty(pass.getUserPhone())){
            throw new MyException(-1,"手机号为空");
        }
        if(StringUtils.isEmpty(pass.getPhoneCode())){
            throw new MyException(-1,"验证码为空");
        }


    }

    public static void userIsEmpty(ViewUser user){
        if(user.getBranchId() == null){
            throw new MyException(-1,"总店机构为空");
        }
        if(StringUtils.isEmpty(user.getUsername())){
            throw new MyException(-1,"用户名为空");
        }
        if(user.getRoleId() == null){
            throw new MyException(-1,"角色为空");
        }
        if(user.getStatus() == null){
            throw new MyException(-1,"状态为空");
        }
        if(StringUtils.isEmpty(user.getRealName())){
            throw new MyException(-1,"真实姓名为空");
        }


    }

    public static void goodsdefIsEmpty(GoodsDef goodsDef){
        if(goodsDef.getBranchId() == null){
            throw new MyException(-1,"总店机构为空");
        }
        if(StringUtils.isEmpty(goodsDef.getGoodsName())){
            throw new MyException(-1,"商品名为空");
        }
        if(StringUtils.isEmpty(goodsDef.getGoodsPic())){
            throw new MyException(-1,"商品图片为空");
        }
        if(StringUtils.isEmpty(goodsDef.getGoodsDescription())){
            throw new MyException(-1,"商品富文本为空");
        }
        if(StringUtils.isEmpty(goodsDef.getGoodsTips())){
            throw new MyException(-1,"商品提示信息为空");
        }
        if(StringUtils.isEmpty(goodsDef.getBuyMethod())){
            throw new MyException(-1,"商品配送方式为空");
        }
        if(goodsDef.getStatus() == null){
            throw new MyException(-1,"状态为空");
        }
        if(goodsDef.getApplyActivityTypeId() != null){
            if(goodsDef.getApplyActivityStartTime() == null || goodsDef.getApplyActivityEndTime() == null){
                throw new MyException(-1,"活动商品必须设定活动时间");
            }
        }

    }

    public static void phoneIslegal(String phone){
        if (StringUtils.isEmpty(phone)) {
            throw new MyException(-1,"号码为空");
        }
        if (phone.length() != 11) {
            throw new MyException(-1,"号码错误");
        }
        Pattern pattern = Pattern.compile("^1[3,5]\\d{9}||18[6,8,9]\\d{8}$");
        Matcher matcher = pattern.matcher(phone);

        if (!matcher.matches()) {
            throw new MyException(-1,"号码错误");
        }
    }

    public static void goodsModelIsEmpty(GoodsModel goodsModel){
//        if(goodsModel.getGoodsDefId() == null){
//            throw new MyException(-1,"商品定义为空");
//        }
        if(StringUtils.isEmpty(goodsModel.getGoodsModelName())){
            throw new MyException(-1,"商品规格名称为空");
        }
        if(goodsModel.getGoodsTotal() == null){
            throw new MyException(-1,"商品数量为空");
        }
        if(goodsModel.getGoodsModelCost() == null){
            throw new MyException(-1,"商品成本价为空");
        }
        if(goodsModel.getGoodsModelSalePrice() == null){
            throw new MyException(-1,"商品销售价为空");
        }
        if(goodsModel.getGoodsModelChannelPrice() == null){
            throw new MyException(-1,"商品渠道价为空");
        }

    }

    public static String listToSqlIn(List<Integer> list){
        String str = "(";
       for(int i=0;i<list.size();i++){
           str += list.get(i) + ",";
       }
       str =  str.substring(0,str.length() - 1);
       str = str + ")";
        return str;
    }

    public static void adverIsEmpty(Advertisement advertisement){
        if(advertisement.getAdvertisementType() == null){
            throw new MyException(-1,"广告类型为空");
        }
        if(StringUtils.isEmpty(advertisement.getAdvertisementName())){
            throw new MyException(-1,"广告名称为空");
        }
        if(StringUtils.isEmpty(advertisement.getAdvertisementUrl())){
            throw new MyException(-1,"广告图片地址为空");
        }
        if(StringUtils.isEmpty(advertisement.getAdvertisementHref())){
            throw new MyException(-1,"广告图片跳转地址为空");
        }
        if(advertisement.getAdvertisementSort() == null){
            throw new MyException(-1,"广告排序为空");
        }
        if(advertisement.getAdvertisementStatus() == null){
            throw new MyException(-1,"状态为空");
        }

    }

    public static void adverIsEmpty(AQiniuFiles aQiniuFiles){
        if(StringUtils.isEmpty(aQiniuFiles.getFileName())){
            throw new MyException(-1,"文件名为空");
        }
//        if(StringUtils.isEmpty(aQiniuFiles.getMimeType())){
//            throw new MyException(-1,"文件类型为空");
//        }
        if(StringUtils.isEmpty(aQiniuFiles.getQiqiuUrl())){
            throw new MyException(-1,"文件七牛地址地址为空");
        }
    }

    public static void catStaticIsEmpty(QueryParamsCatStatic queryParamsCatStatic){
        if(queryParamsCatStatic.getPage() == null){
            throw new MyException(-1,"分页页数为空");
        }
        if(queryParamsCatStatic.getLimit() == null){
            throw new MyException(-1,"分页数量为空");
        }
    }

    public static void letBuyIsEmpty(QueryParamsLetBuy queryParamsLetBuy){
        if(queryParamsLetBuy.getPage() == null){
            throw new MyException(-1,"分页页数为空");
        }
        if(queryParamsLetBuy.getLimit() == null){
            throw new MyException(-1,"分页数量为空");
        }
        if(StringUtils.isEmpty(queryParamsLetBuy.getBuyMethod())){
            throw new MyException(-1,"配送类型为空");
        }
    }

    public static void goodsListIsEmpty(QueryParamsGoodsList queryParamsLetBuy){
        if(queryParamsLetBuy.getPage() == null){
            throw new MyException(-1,"分页页数为空");
        }
        if(queryParamsLetBuy.getLimit() == null){
            throw new MyException(-1,"分页数量为空");
        }
        if(StringUtils.isEmpty(queryParamsLetBuy.getBuyMethod())){
            throw new MyException(-1,"配送类型为空");
        }
    }

    public static void goodsBrowsingIsEmpty(QueryParamsGoodsBrowsing queryParamsGoodsBrowsing){
        if(queryParamsGoodsBrowsing.getPage() == null){
            throw new MyException(-1,"分页页数为空");
        }
        if(queryParamsGoodsBrowsing.getLimit() == null){
            throw new MyException(-1,"分页数量为空");
        }
        if(queryParamsGoodsBrowsing.getGoodsDefId() == null){
            throw new MyException(-1,"商品id为空");
        }
    }

    public static void recommendGoodsIsEmpty(RecommendGoods recommendGoods){
        if(recommendGoods.getGoodsDefId() == null){
            throw new MyException(-1,"商品id为空");
        }
        if(recommendGoods.getActivityTypeId() == null){
            throw new MyException(-1,"推荐类型为空");
        }
        if(recommendGoods.getActivityStartTime() == null){
            throw new MyException(-1,"推荐开始时间为空");
        }
        if(recommendGoods.getActivityEndTime() == null){
            throw new MyException(-1,"推荐结束时间为空");
        }
        if(recommendGoods.getStatus() == null){
            throw new MyException(-1,"状态为空");
        }
    }

    public static void manliSpaceIsEmpty(ManliSpace manliSpace){
        if(StringUtils.isEmpty(manliSpace.getContent())){
            throw new MyException(-1,"动态文本内容不能为空");
        }
        if(StringUtils.isEmpty(manliSpace.getTypeTag())){
            throw new MyException(-1,"动态类型不能为空");
        }else{
            try {
                JSON.parseArray(manliSpace.getTypeTag());
            } catch (Exception e) {
                throw new MyException(-1,"动态类型应该传数组");
            }
        }
        if(StringUtils.isEmpty(manliSpace.getThemeTag())){
            throw new MyException(-1,"动态主题不能为空");
        }else{
            try {
                JSON.parseArray(manliSpace.getThemeTag());
            } catch (Exception e) {
                throw new MyException(-1,"动态主题应该传数组");
            }
        }

    }

    public static void manliSpaceLogIsEmpty(ManliSpaceLog manliSpaceLog){
        if(manliSpaceLog.getSpaceId() == null){
            throw new MyException(-1,"动态id为空");
        }
        if(StringUtils.isEmpty(manliSpaceLog.getReviewContent())){
            throw new MyException(-1,"评论文本内容不能为空");
        }
    }

    public static void spaceListIsEmpty(QueryParamsSpaceList queryParamsSpaceList){
        if(queryParamsSpaceList.getLimit() == null){
            throw new MyException(-1,"分页参数为空");
        }
        if(queryParamsSpaceList.getPage() == null){
            throw new MyException(-1,"分页参数为空");
        }
        if(queryParamsSpaceList.getType() == null){
            throw new MyException(-1,"查询类型为空");
        }
    }

    public static void checkPayPriceIsEmpty(QueryParamsCheckPayPrice params){
        if(params.getActivityType() == null){
            throw new MyException(-1,"活动类型为空");
        }
        if(params.getGoodsDefId() == null){
            throw new MyException(-1,"商品id为空");
        }
        if(params.getGoodsModelId() == null){
            throw new MyException(-1,"商品规格id为空");
        }
        if(StringUtils.isEmpty(params.getUnionid())){
            throw new MyException(-1,"unionid为空");
        }
    }

    public static void appraiseLogIsEmpty(AppraiseLog params){
        if(params.getBranchId() == null){
            throw new MyException(-1,"店铺id为空");
        }

        if(params.getGoodsDefId() == null){
            throw new MyException(-1,"商品id为空");
        }
        if(params.getGoodsModelId() == null){
            throw new MyException(-1,"商品规格id为空");
        }
        if(StringUtils.isEmpty(params.getAppraiseMsg())){
            throw new MyException(-1,"评价内容为空");
        }
        if(params.getAppraiseScore() == null){
            throw new MyException(-1,"商品评分为空");
        }
        if(params.getAppraiseTasteScore() == null){
            throw new MyException(-1,"商品味道评分为空");
        }
        if(params.getAppraisePackScore() == null){
            throw new MyException(-1,"商品包装评分为空");
        }
        if(StringUtils.isEmpty(params.getOrderNoMain())){
            throw new MyException(-1,"总订单号为空");
        }
        if(StringUtils.isEmpty(params.getOrderNoSub())){
            throw new MyException(-1,"子订单号为空");
        }
    }

    public static void couponGrantIsEmpty(CouponGrant couponGrant) {
        if(StringUtils.isEmpty(couponGrant.getStockName())){
            throw new MyException(-1,"批次名称为空");
        }
        if(StringUtils.isEmpty(couponGrant.getAvailableBeginTime())){
            throw new MyException(-1,"批次开始时间为空");
        }
        if(StringUtils.isEmpty(couponGrant.getAvailableEndTime())){
            throw new MyException(-1,"批次结束时间为空");
        }
        if(couponGrant.getMaxCoupons() == null){
            throw new MyException(-1,"最大发券数为空");
        }
        if(couponGrant.getMaxAmountByDay() == null){
            throw new MyException(-1,"单天最高消耗金额为空");
        }
        if(couponGrant.getMaxCouponsPerUser() == null){
            throw new MyException(-1,"单个用户可领个数为空");
        }
        if(StringUtils.isEmpty(couponGrant.getDescription())){
            throw new MyException(-1,"活动规则为空");
        }
        if(couponGrant.getCouponAmount() == null){
            throw new MyException(-1,"面额为空");
        }
        if(couponGrant.getTransactionMinimum() == null){
            throw new MyException(-1,"使用券金额门槛为空");
        }
    }

    public static void queryParamsCouponBatchIsEmpty(QueryParamsCouponBatch queryParamsCouponBatch) {
        if(queryParamsCouponBatch.getPage() == null){
            throw new MyException(-1,"分页参数为空");
        }
        if(queryParamsCouponBatch.getLimit() == null){
            throw new MyException(-1,"分页参数为空");
        }
    }

    public static void getCouponListByClass1IsEmpty(ManTaoTaoCouponsByClass t) {
        if(t.getLongitude() == null){
            throw new MyException(-1,"经纬度为空");
        }
        if(t.getLatitude() == null){
            throw new MyException(-1,"经纬度为空");
        }
        if(t.getMajorTypeId() == null){
            throw new MyException(-1,"分类id为空");
        }
    }

    public static void getCouponListByKeyWord1IsEmpty(ManTaoTaoCouponsByKeyWord t) {
        if(t.getLongitude() == null){
            throw new MyException(-1,"经纬度为空");
        }
        if(t.getLatitude() == null){
            throw new MyException(-1,"经纬度为空");
        }
    }
}
